package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetMealMapper setMealMapper;
    @Override
    public BusinessDataVO businessData(LocalDateTime beginTime, LocalDateTime endTime) {
        Integer newUser = userMapper.countNewUser(beginTime, endTime);
        Map map = new HashMap();
        map.put("beginTime", beginTime);
        map.put("endTime", endTime);
        Integer totalOrder = orderMapper.countOrders(map);
        map.put("status", Orders.COMPLETED);
        Integer validOrder = orderMapper.countOrders(map);
        Double orderCompletionRate=0.0;
        if(totalOrder!=0){
            orderCompletionRate = (double) validOrder/totalOrder;
        }
        Double orderMoney = orderMapper.turnoverStatistics(map);
        orderMoney = orderMoney==null?0.0:orderMoney;
        Double averagePrice = 0.0;
        if(validOrder!=0){
            averagePrice = orderMoney/validOrder;
        }
        BusinessDataVO businessDataVO = BusinessDataVO.builder()
                .newUsers(newUser)
                .orderCompletionRate(orderCompletionRate)
                .turnover(orderMoney)
                .unitPrice(averagePrice)
                .validOrderCount(validOrder)
                .build();
        return businessDataVO;
    }

    @Override
    public OrderOverViewVO overviewOrders() {
        LocalDate now = LocalDate.now();
        LocalDateTime beginTime = LocalDateTime.of(now, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(now, LocalTime.MAX);

        Map map = new HashMap();
        map.put("beginTime", beginTime);
        map.put("endTime", endTime);
        Integer allOrders = orderMapper.countOrders(map);//总订单
        map.put("status", Orders.COMPLETED);
        Integer completedOrders = orderMapper.countOrders(map);//已完成
        map.replace("status",Orders.CANCELLED);
        Integer cancelledOrders = orderMapper.countOrders(map);//已取消
        map.replace("status",Orders.CONFIRMED);
        Integer confirmedOrders = orderMapper.countOrders(map);//待派送
        map.replace("status",Orders.TO_BE_CONFIRMED);
        Integer toBeConfirmedOrders = orderMapper.countOrders(map);//待接单
        OrderOverViewVO orderOverViewVO = OrderOverViewVO.builder()
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(confirmedOrders)
                .waitingOrders(toBeConfirmedOrders)
                .build();
        return orderOverViewVO;
    }

    @Override
    public DishOverViewVO overviewDishes() {
        Integer openDishCount = dishMapper.countDishes(StatusConstant.ENABLE);
        Integer closeDishCount = dishMapper.countDishes(StatusConstant.DISABLE);
        DishOverViewVO dishOverViewVO = DishOverViewVO.builder()
                .sold(openDishCount)
                .discontinued(closeDishCount)
                .build();
        return dishOverViewVO;
    }

    @Override
    public SetmealOverViewVO overviewSetmeals() {
        Integer openSetmealCountCount = setMealMapper.countSetmeals(StatusConstant.ENABLE);
        Integer closeSetmealCount = setMealMapper.countSetmeals(StatusConstant.DISABLE);
        SetmealOverViewVO setmealOverViewVO = SetmealOverViewVO.builder()
                .sold(openSetmealCountCount)
                .discontinued(closeSetmealCount)
                .build();
        return setmealOverViewVO;
    }

}
